<?php
class Main extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('post_model');
        $this->load->model('slide_model');
    }

    public function index() {
        if (isset($_SESSION['userinfo']['nickname'])) {
            $this->mainpage();
        } else {
            $this->load->view('header');
            $this->load->view('index');
            $this->load->view('footer');
        }
    }

    public function mainpage() {
        $this->load->helper('util');

        $topten = $this->post_model->get_top_ten();
        if ($topten !== NULL) {
            foreach ($topten as &$row) {
                $row['post_topic_value'] = value_to_topic($row['post_topic']);
                $row['post_constel'] = value_to_constel($row['post_constel']);
                $row['post_gender'] = value_to_gender($row['post_gender']);
                if ($row['photo_location_cache'] == null)
                    $row['photo_location_cache'] = "assets/img/construct.png";
            }
        }
        $topdata['topten'] = $topten;
        $data['topdata'] = $topdata;

        $data['slides'] = $this->slide_model->get_mainpage();

        $this->load->view('header');
        $this->load->view('navbar');
        $this->load->view('main/main_page', $data);
        $this->load->view('footer');
    }

    public function generate_top_ten_thumb($file_name) {
        $data = array();
        $data['code'] = 0;

        $data['filename'] = $file_name;
        if(!empty($file_name)) {
            $this->load->library('image_lib');
            list($width, $height) = getimagesize('upload/'.$file_name);
            $data['code'] = -1;
            if( !empty($width) && !empty($height) ) {
                if( !is_dir('upload/topten') ) {
                        mkdir('upload/topten', 0777, TRUE); //判断缩略图文件夹是否存在，不存在则创建
                }

                $config['image_library'] = 'gd2';
                $config['source_image'] = 'upload/'.$file_name;
                $config['new_image'] = 'upload/topten/'.$file_name;
                $config['maintain_ratio'] = TRUE;
                if($width*3 >= $height*4)
                {
                    $config['master_dim'] = 'height';
                }else{
                    $config['master_dim'] = 'width';
                }
                $config['width'] = 135;
                $config['height'] = 101;
                $this->image_lib->initialize($config);
                $this->image_lib->resize();

                $config['source_image'] = 'upload/topten/'.$file_name;
                $config['new_image'] = NULL;
                $config['maintain_ratio'] = FALSE;
                if($width*3 >= $height*4)
                {
                    $config['x_axis'] = floor(($width * 101 / $height - 135)/2);
                }else{
                    $config['y_axis'] = floor(($height * 135 / $width - 101)/2);
                }
                $this->image_lib->initialize($config);
                $this->image_lib->crop();

                $data['code'] = 1;
                $data['path'] =  'upload/topten/'.$file_name;
            }
        }
        $this->output->set_content_type('application/json')
                ->set_output(json_encode($data));
    }
}
?>